#include "UDPPort.h"
#include "StunAgent.h"

using namespace cm_nat;


class TestStunAgentHandler: public StunAgentHandler
{
public:
    void handleReceive(StunAgent* p_agent, ByteBuffer* data)
    {

    }

    void handleNatResult(StunAgent* p_agent, UInt32 result)
    {
        if( result == StunAgentHandler::NatBindSuccess ){
            std::cout<<"bind success\n";
            p_agent->keepaliveNat();
            std::cout<< p_agent->getNatReflex().toString()<<std::endl;
        }else{
            std::cout<<"bind fail\n";
        }

    }

    void handleNatUpdate(StunAgent* p_agent, SocketAddress& sa)
    {
        std::cout <<"nat update\n";

    }

    void handlePeerCheckResult(StunAgent* p_agent, UInt16 result)
    {
        if( result == StunAgentHandler::PeerCheckSuccess){
            std::cout<<"check connect success";
            p_agent->keepalivePeer();
        }else{
            std::cout<<"check connection fail\n";
        }

    }

    void handlePeerLostConnection(StunAgent* p_agent, UInt16 result)
    {
        std::cout<<"PeerLostConnection\n";
    }
};


int main()
{
    SocketReactor reactor;
    reactor.setTimeout(Poco::Timespan(1,0,0,0,0));

    ByteBufferPool bpool;
    bpool.init(10, 200);

    //SocketAddress stunSa("192.168.0.111","3478");
    SocketAddress stunSa("stun.l.google.com","19302");

    //SocketAddress la("192.168.0.111", 800);
    SocketAddress la("192.168.1.99", 800);
    //SocketAddress la("192.168.1.99", 3478);

    IPAddress ip("192.168.1.99");

    UDPPort port(reactor, &bpool);

    TestStunAgentHandler agentHandler;

    StunAgent agent(&port,&bpool);
    agent.registerHandler( &agentHandler );

    port.registerHandler(&agent);

    port.setup(la);
    //port.setup(ip);

    std::cout<< port.getAddress().toString();

    Thread t;
    
    t.start(reactor);
    
    //reactor.run();

    std::string input;

    //SocketAddress stunSa("192.168.1.99", 4000);
    //while(1)
   // session.natSetup(stunSa);

    //SocketAddress peer("117.88.4.15", 43985);
    //session.iceConnCheck( peer);
    agent.setPubStunServer(stunSa);

    agent.setupNatBind();

    //agent.checkConnectPeer(stunSa);


    std::cin>>input;

    return 0;
}